﻿using System;
using System.Threading;

namespace 线程安全问题
{
    internal class Program
    {
        private static int total = 10_000;
        private static int sum = 0;
        
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(ThreadMethod);
            Thread thread2 = new Thread(ThreadMethod);

            // 启动线程
            thread1.Start();
            thread2.Start();


            // 等待线程执行完成
            thread1.Join();
            thread2.Join();

            // 执行主线程的代码
            Console.WriteLine($"期望结果是20000，实际结果是{sum}"); // 期望结果是20000，实际结果是17668

            Console.WriteLine("Hello World!");
        }


        /// <summary>
        /// 线程委托
        /// </summary>
        private static void ThreadMethod()
        {

            for (int i = 0; i < total; i++)
            {
                sum++;
            }

        }



    }
}
